package com.media.api;

import com.alibaba.fastjson.JSON;
import com.base.model.R;
import com.media.model.Favorite;
import com.media.model.Media;
import com.media.model.User;
import com.media.service.FavoriteService;
import com.media.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: 小何
 * @Description: TODO
 * @DateTime: 2023/10/30 16:49
 **/
@RestController
@RequestMapping("/favorite")
public class FavoriteController {
    @Autowired
    private FavoriteService favoriteService;

    @Autowired
    private RedisTemplate redisTemplate;

    private static final String REDIS_FAVORITE_PRE = "favorite_";

    @PostMapping("/video")
    @PreAuthorize("hasAuthority('favoriteVideo')")
    public R<String> favorite(@RequestBody Media media) {
        //1.获取用户Id
        User user = SecurityUtils.getUser();
        Integer id = user.getId();
        //2.删除Redis
        redisTemplate.delete(REDIS_FAVORITE_PRE + id);
        //3.插入收藏信息
        Favorite favorite = new Favorite();
        favorite.setMediaId(media.getId());
        favorite.setTitle(media.getTitle());
        favorite.setMd5(media.getMd5());
        favorite.setUserId(id);
        favoriteService.save(favorite);
        return R.success("收藏成功");
    }

    @GetMapping("/listFavorite")
    @PreAuthorize("hasAuthority('listFavorite')")
    public R<List<Favorite>> listFavorite() {
        //1.获取用户Id
        User user = SecurityUtils.getUser();
        Integer id = user.getId();
        if (redisTemplate.hasKey(REDIS_FAVORITE_PRE + id)) {
            String json = (String) redisTemplate.opsForValue().get(REDIS_FAVORITE_PRE + id);
            List<Favorite> list = JSON.parseObject(json, ArrayList.class);
            return R.success(list);
        }
        //2.通过用户Id获取收藏视频
        R<List<Favorite>> listR = favoriteService.listFavorite(id);
        redisTemplate.opsForValue().set(REDIS_FAVORITE_PRE + id, JSON.toJSONString(listR.getData()));
        return listR;
    }

    @PostMapping("/cancel/{mediaId}")
    @PreAuthorize("hasAuthority('favoriteCancel')")
    public R<String> cancelFavorite(@PathVariable String mediaId) {
        //1.获取用户Id
        User user = SecurityUtils.getUser();
        Integer id = user.getId();
        //2.删除Redis
        redisTemplate.delete(REDIS_FAVORITE_PRE + id);
        //3.用户Id和视频Id删除收藏记录
        return favoriteService.cancelFavorite(mediaId, id);
    }

    @PostMapping("/judgeFavorite/{mediaId}")
    @PreAuthorize("hasAuthority('judgeFavorite')")
    public R<Boolean> judgeFavorite(@PathVariable String mediaId) {
        //获取用户Id
        User user = SecurityUtils.getUser();
        Integer userId = user.getId();
        //判断用户是否收藏该视频
        return favoriteService.judgeFavorite(mediaId, userId);
    }


}
